<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script type="text/javascript" src="<c:url value='/js/plug/area/load_area.js'/>"></script>
<%@ taglib prefix="se" uri="http://zc.security/acl"%>

<div id="cc_buyers" class="easyui-layout" style="width:100%;height:100%;" fit="true">
	<div data-options="region:'north',title:'采购商管理',split:true" style="height:70px;background-color:#e6edfd;">
    	<form action="" id="searchForm" name="searchForm">
    	<table class="screen">
			<tr>
				<td class="screen-n"><span>中文名称</span></td>
				<td class="screen-n">
					<input type="text" id="s_chsName" class="screen-input" style="width:200px" onkeydown="pms.kd13('initBuyers')">
				</td>
				<td class="screen-n"><span>是否存在登录账号</span></td>
				<td class="screen-n">
				    <select name="isMember" id="isMember" style="width:80px;" class="easyui-combobox" panelheight="auto">
				       <option value="">全部</option>
					   <option value="0">否</option>
					   <option value="1">是</option>
				    </select>
				</td>
				<td class="screen-n"><span>所属地区</span></td>
				<td class="screen-n">
						<div id="loc" >
     		                  <select name="province" id="province" style="width:100px;">
     		                     <option value="0">选择省</option>
     		                  </select>   
     		                  <select name="city" id="city" style="width:100px;">
     		                     <option value="0">选择市</option>
     		                  </select>   
     		                  <select name="district" id="district" style="width:100px;">
     		                     <option value="0">选择地区</option>
     		                  </select>
     	                  </div>
				</td>
				<td class="screen-n">
					<input type="button" class="button" value="查询" onclick="initBuyers()">
					<input type="button" class="button" value="重置" onclick="pms.resetForm('searchForm')" >
				    <input type="button" class="button" value="导出" onclick="loadExecl('initStoreOutCheckTableExcel')" >
				</td>
			</tr>
		</table>
		</form>
    </div>
    <div data-options="region:'center',split:true" style="width: 100%;height: 100%">
		<table id="tableGrid"></table>
	</div>
</div>  
<script>
var toolbar = [
<se:hasPermissions actCode="ADD" >
 {
	text:'采购商录入',
    iconCls:'ext-icon-note_add',
    handler:function(){
    	openBuyerAddWindow();
    }
},
</se:hasPermissions>
<se:hasPermissions actCode="APPROVE" >
{
	text:'采购商审核',
    iconCls:'ext-icon-key',
    handler:function(){
    	var rows = buyersTable.getCheckedData();
		if(rows==null || rows.length==0){
			alertWarning("请选中要审核的数据!");
			return;
		}
		//采购商审核
		if(confirm("确定批量审核?")){
			var ids = [];
			for(var i=0; i<rows.length; i++){
				ids.push(rows[i].buyerId);
			}
			var buyerIdList = ids.toString();
			var param = {"buyerIdList":buyerIdList};
			$.pmsAjax({
				url:'<c:url value="/buyer/editMultipleEmCBuyersStatus.do"/>',
				data:param,
				success:function(data){
					if(data.code=="200"){
						alertShow("批量审批成功！");
						buyersTable.refresh();
					}else{
						alertError(data.result);
					}
				}
			});
		}
    }
},
</se:hasPermissions>
<se:hasPermissions actCode="DELETE" >
{
	text:'采购商删除',
    iconCls:'ext-icon-note_delete',
    handler:function(){
    	var rows = buyersTable.getCheckedData();
		if(rows==null || rows.length==0){
			alertWarning("请选中要删除的数据!");
			return;
		}
		if(confirm("确定批量删除?")){
			var ids = [];
			for(var i=0; i<rows.length; i++){
				ids.push(rows[i].buyerId);
			}
			var buyerIdList = ids.toString();
			var param = {"buyerIdList":buyerIdList};
			$.pmsAjax({
				url:'<c:url value="/buyer/deleteMultipleEmCBuyers.do"/>',
				data:param,
				success:function(data){
					if(data.code=="200"){
						alertShow("批量删除成功！");
						buyersTable.refresh();
					}else{
						alertError(data.result);
					}
				}
			});
		}
    }
}
</se:hasPermissions>
];
var buyersTable;
$(function(){
	initBuyers();
});
function getParams(){
	var queryParamss = {"chsName":$('#s_chsName').val(), 
			"areaCode":$('#loc').getLocation('all').code,
			"isMember":$('#isMember').combobox('getValue')};
	return queryParamss;
}
function initBuyers(){
	buyersTable = $('#tableGrid').pDataGrid({
		toolbar:toolbar,
		columns:[[
				   {field:'ck',checkbox:true},
	               {field:'chsName',title:'采购商名称',align:'center'},
	               {field:'buyerCode',title:'采购商编号',align:'center'},
	               {field:'spell',title:'拼音码',align:'center'},
	               {field:'salesman',title:'销售员',align:'center'},
	               {field:'phone',title:'电话',align:'center'},
	               {field:'areaName',title:'所属地区',align:'areaName'},
	               {field:'address',title:'地址',align:'center'},
	               {field:'approvedStatus',title:'是否有效',align:'center',formatter:function(value){
						if(value=="0"){
							return '无效';
						}else{
							return '有效';
						}
		           }},
	               {field:'isValid',title:'是否审核',align:'center',formatter:function(value){
						if(value=="0"){
							return '未审';
						}else{
							return '审核';
						}
		           }},
		           {field:'action',title : '操作',align : 'center',
						formatter : function(value, row, index) {
							var str = '';
							<se:hasPermissions actCode="DELETE" >
								str += ' <img class="iconImg ext-icon-note_edit" title="编辑" onclick="openBuyerUpdateWindow('+row.buyerId+');"/>';
							</se:hasPermissions>
							<se:hasPermissions actCode="MAINTAIN" >	
								str += ' <img class="iconImg ext-icon-group_add" title="添加用户" onclick="AddBuyerUserWindow('+row.buyerId+');"/>';
							</se:hasPermissions>
							<se:hasPermissions actCode="DELETE" >		
								str += ' <img class="iconImg ext-icon-note_delete" title="删除" onclick="deleteBuyer('+row.buyerId+',' + index + ');"/>';
							</se:hasPermissions>
								return str;
						}
					} 
		          ]],
		    url:'<c:url value="/buyer/getBuyerList.do"/>',  
		    pagination:true,
		    pageSize:30,
		    fitColumns:true,
		    singleSelect:false,
		    queryParams:getParams(),
			onDblClickRow:function(index,row){
				openBuyerUpdateWindow(row.buyerId);
			},
			completeCallback:function(data){
			},
			onLoadSuccess : function(data) {
				var panel = $(this).datagrid('getPanel');
				var tr = panel.find('div.datagrid-body tr');
				tr.each(function() {
					var td = $(this).children('td[field="chsName"]');
					td.children("div").css({
						"text-align" : "left"
					});
				});
				tr.each(function() {
					var td = $(this).children('td[field="areaName"]');
					td.children("div").css({
						"text-align" : "left"
					});
				});
				tr.each(function() {
					var td = $(this).children('td[field="address"]');
					td.children("div").css({
						"text-align" : "left"
					});
				});
			}
		    
	}); 
}

function openBuyerAddWindow(){
	$.pmsDialog({
		id:"w_buyer_detail",
		title:'新增采购商',
		isIf:'true',
		method:'post',
		href:'<c:url value="/views/jsp/customer/buyer/buyer_detail.jsp"/>',
		width:850,
	});
}
function openBuyerUpdateWindow(code){
	var params = {"code":code};
	$.pmsDialog({
		id:"w_buyer_detail",
		title:'修改采购商',
		isIf:'true',
		method:'post',
		params:params,
		href:'<c:url value="/views/jsp/customer/buyer/buyer_detail.jsp"/>',
		width:850
	}); 
}
function AddBuyerUserWindow(code){
	var params = {"code":code};
	$.pmsDialog({
		id:"w_adduser",
		title:'添加用户',
		method:'post',
		params:params,
		href:'<c:url value="/views/jsp/customer/buyer/buyer_adduser_s.jsp"/>',
		width:550,
		height:500
	}); 
}
function deleteBuyer(code,i){
	if(confirm("确定删除?")){
		var param = {"buyerId":code};
		$.pmsAjax({
			url:'<c:url value="/buyer/deleteBuyer.do"/>',
			data:param,
			success:function(data){
				if(data.code=="200"){
					alertShow("删除成功");
					buyersTable.refresh();
				}else{
					alertError(data.result);
				}
			}
		});
	}
}

$("#loc").LocationSelect({
	data:"<c:url value='/js/plug/area/areas.json' />"
});
//导出
function initStoreOutCheckTableExcel(){
	loadExcelCallBack('<c:url value="/buyer/searchPageBuyerExcel.do"/>',getParams());
} 
</script>
